﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCPerson_JobNatureDAL:ItblCPerson_JobNature
    {
        HKEA_CMSEntities model;
        tblCPerson_JobNature jobnature;

        public tblCPerson_JobNatureDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCPerson_JobNatureDAL GetInstance()
        {
            return new tblCPerson_JobNatureDAL();
        }

        public int InsertENT(tblCPerson_JobNature ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCPerson_JobNature.AddObject(ent);
            return model.SaveChanges();
        }

        public int SaveList(List<int> AddToJobList, List<int> DelToJobList, int cpuid, string LastModifyBy)
        {
            foreach (var temp in AddToJobList)
            {
                var tempValue = model.tblCPerson_JobNature.Where(a => a.ContactPersonUID == cpuid).
                    Where(a => a.JobNatureUID == temp).
                    Where(a => a.Deleted == 0).FirstOrDefault();
                if (tempValue == null)
                {
                    var ent = new tblCPerson_JobNature();
                    ent.JobNatureUID = temp;
                    ent.ContactPersonUID = cpuid;
                    ent.CreationDate = DateTime.Now;
                    ent.Deleted = 0;
                    ent.LastModifyBy = LastModifyBy;
                    ent.LastModifyDate = DateTime.Now;
                    model.tblCPerson_JobNature.AddObject(ent);
                }
            }
            foreach (var temp in DelToJobList)
            {
                var result = model.tblCPerson_JobNature.Where(a => a.ContactPersonUID == cpuid).
                    Where(a => a.JobNatureUID == temp).
                    Where(a => a.Deleted == 0).FirstOrDefault();
                if (result != null)
                {
                    result.LastModifyDate = DateTime.Now;
                    result.LastModifyBy = LastModifyBy;
                    result.Deleted = 1;
                }
            }
            return model.SaveChanges();
        }

        public int InsertENT(List<int> jobNatureuidList, int cpuid, string LastModifyBy)
        {
            var toDel = model.tblCPerson_JobNature.Where(a => a.ContactPersonUID == cpuid);
            if (toDel.Count() > 0)
            {
                foreach (var temp in toDel)
                {
                    model.tblCPerson_JobNature.DeleteObject(temp);
                }
            }
            
            foreach (var attuid in jobNatureuidList)
            {
                var tempENT = new tblCPerson_JobNature();
                tempENT.JobNatureUID = attuid;
                tempENT.ContactPersonUID = cpuid;
                tempENT.Deleted = 0;
                tempENT.LastModifyBy = LastModifyBy;
                tempENT.CreationDate = DateTime.Now;
                tempENT.LastModifyDate = DateTime.Now;
                model.tblCPerson_JobNature.AddObject(tempENT);
            }
            return model.SaveChanges();
        }

        public List<tblCPerson_JobNature> GetListByENT(tblCPerson_JobNature ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCPerson_JobNature.Where(a => a.CPerson_JobNatureUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CPerson_JobNatureUID > 0)
                result = result.Where(a => a.CPerson_JobNatureUID == ent.CPerson_JobNatureUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblCPerson_JobNature> GetListByENT(tblCPerson_JobNature ent)
        {
            var result = model.tblCPerson_JobNature.Where(a => a.CPerson_JobNatureUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CPerson_JobNatureUID > 0)
                result = result.Where(a => a.CPerson_JobNatureUID == ent.CPerson_JobNatureUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblCPerson_JobNature ent)
        {
            var result = model.tblCPerson_JobNature.Where(a => a.CPerson_JobNatureUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CPerson_JobNatureUID > 0)
                result = result.Where(a => a.CPerson_JobNatureUID == ent.CPerson_JobNatureUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblCPerson_JobNature ent)
        {
            jobnature = model.tblCPerson_JobNature.Where(a=>a.CPerson_JobNatureUID==ent.CPerson_JobNatureUID).FirstOrDefault();
            if (jobnature != null)
            {
                jobnature.Deleted = ent.Deleted;
                jobnature.LastModifyBy = ent.LastModifyBy;
                jobnature.LastModifyDate = DateTime.Now;
                jobnature.ContactPersonUID = ent.ContactPersonUID;
                jobnature.JobNatureUID = ent.JobNatureUID;
                jobnature.Remarks = ent.Remarks;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblCPerson_JobNature ent)
        {
            var temp = model.tblCPerson_JobNature.Where(a => a.CPerson_JobNatureUID > 0);
            if (ent.CPerson_JobNatureUID > 0)
                temp = temp.Where(a=>a.CPerson_JobNatureUID==ent.CPerson_JobNatureUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCPerson_JobNature.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }

        public List<tblCPerson_JobNature> GetList(tblCPerson_JobNature ent, List<int> withOutList)
        {
            var result = model.tblCPerson_JobNature.Where(a => a.CPerson_JobNatureUID > 0);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (withOutList.Count > 0)
                result = result.Where(a => !withOutList.Contains(a.CPerson_JobNatureUID));
            return result.ToList();
        }
    }
}
